package com.wing.common.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.dao.ContractDao;
import com.wing.common.model.entity.Contract;
import com.wing.common.model.entity.ContractChangeRecord;
import com.wing.common.model.request.ContractForm;
import com.wing.common.model.response.ContractVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 合同-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ContractServiceImpl extends ServiceImpl<ContractDao, Contract> implements ContractService {

    @Autowired
    private ContractChangeRecordService contractChangeRecordService;

    @Override
    @Transactional(readOnly = false)
    public int add(ContractForm contractForm) {
        //校验
        Contract contract = new Contract();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(Contract.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(contractForm, contract, copyOptions);
        if (StringUtils.isNotEmpty(contractForm.getAttachmentPdf())) {
            contract.setAttachment(contractForm.getAttachmentPdf());
        } else if (StringUtils.isNotEmpty(contractForm.getAttachmentImage())) {
            contract.setAttachment(contractForm.getAttachmentImage());
        }
        int i = baseMapper.insert(contract);
        if (i > 0) {
            // 添加合同变更记录
            ContractChangeRecord contractChangeRecord = new ContractChangeRecord();
            CopyOptions contractChangeRecordCopyOptions = CopyOptions.create()
                    .setEditable(ContractChangeRecord.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(contractForm, contractChangeRecord, contractChangeRecordCopyOptions);
            contractChangeRecord.setContractId(contract.getId());
            if (StringUtils.isNotEmpty(contractForm.getAttachmentPdf())) {
                contractChangeRecord.setAttachment(contractForm.getAttachmentPdf());
            } else if (StringUtils.isNotEmpty(contractForm.getAttachmentImage())) {
                contractChangeRecord.setAttachment(contractForm.getAttachmentImage());
            }
            contractChangeRecordService.save(contractChangeRecord);
        }
        return i;
    }

    @Override
    public List<ContractVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ContractVO> pageList4app(Page page, Map<String, Object> params) {
        List<ContractVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ContractVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
